﻿using Microsoft.EntityFrameworkCore;
using STF.Blog.IRepository;
using STF.Blog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Repository
{
    public class BaseManager<T> : IBaseManager<T> where T : BaseEntity, new()
    {
        private BlogContext _bc;
        public BaseManager(BlogContext bc)
        {
            _bc = bc;
        }
        public async Task<int> AddAsync(T model)
        {
            _bc.Entry(model).State = EntityState.Added;
            return await _bc.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T model)
        {
            _bc.Entry(model).State = EntityState.Deleted;
            return await _bc.SaveChangesAsync();
        }

        public async Task<List<T>> GetAllAsync(T model)
        {
            return await _bc.Set<T>().ToListAsync();
        }

        public async Task<T> GetById(int id)
        {
            return await _bc.Set<T>().FindAsync(id);
        }

        public async Task<List<T>> GetChooseAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await _bc.Set<T>().Where(whereLambda).ToListAsync();
        }

        public async Task<int> UpdateAsync(T model)
        {
            _bc.Entry(model).State = EntityState.Modified;
            return await _bc.SaveChangesAsync();
        }
    }
}
